//
//  TestScrollPageViewController.m
//  MyDemos
//
//  Created by iMac on 2018/3/8.
//  Copyright © 2018年 iMac. All rights reserved.
//

#import "TestScrollPageViewController.h"
#import "SPScrollPageView.h"
#import "WDSliderTitleView.h"
#import "TestScrollPageRedViewController.h"
#import "TestScrollPageBlueViewController.h"
#import "TestScrollPageGreenViewController.h"
#import "WDSliderContenerCollectionView.h"
#import "WDSliderContenerScrollView.h"



@interface TestScrollPageViewController ()<SPScrollPageViewDelegate>

@property (nonatomic, strong) TestScrollPageRedViewController *redViewController;
@property (nonatomic, strong) TestScrollPageBlueViewController *blueViewController;
@property (nonatomic, strong) TestScrollPageGreenViewController *greenViewController;
@property (nonatomic, strong) TestScrollPageRedViewController *redViewController2;
@property (nonatomic, strong) TestScrollPageBlueViewController *blueViewController2;
@property (nonatomic, strong) TestScrollPageGreenViewController *greenViewController2;
@property (nonatomic, strong) NSArray *childControllers;
@property (nonatomic, strong) WDSliderTitleView *sliderTitleView;
@property (nonatomic, strong) WDSliderContenerCollectionView *sliderContenerView;
@property (nonatomic, strong) SPScrollPageView *scrollPageView;
@property (nonatomic, strong) WDSliderContenerScrollView *sliderContenerScrollView;

@end

@implementation TestScrollPageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self addDismissButton];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    [self setupViewDidload];
}

- (void)setupViewDidload {
    self.redViewController = [[TestScrollPageRedViewController alloc] init];
    self.blueViewController = [[TestScrollPageBlueViewController alloc] init];
    self.greenViewController = [[TestScrollPageGreenViewController alloc] init];
    self.redViewController2 = [[TestScrollPageRedViewController alloc] init];
    self.blueViewController2 = [[TestScrollPageBlueViewController alloc] init];
    self.greenViewController2 = [[TestScrollPageGreenViewController alloc] init];
    self.childControllers = @[self.redViewController, self.blueViewController, self.greenViewController];
    WDSliderTitleView *titleView = [[WDSliderTitleView alloc] initWithFrame:CGRectMake(0, 100, SCREEN_WIDTH, 50)];
    titleView.buttonTitles = @[@"red", @"blue", @"green", @"red", @"blue", @"green"];
    self.sliderTitleView = titleView;
    [self.view addSubview:titleView];
    
    
    WDSliderContenerScrollView *scroll = [WDSliderContenerScrollView wdSliderContenerScrollViewWithFrame:CGRectMake(0, 150, SCREEN_WIDTH, 300) views:@[self.redViewController.view,  self.blueViewController.view, self.greenViewController.view, self.redViewController2.view,  self.blueViewController2.view, self.greenViewController2.view] selectIndex:0];
    [self.view addSubview:scroll];
    self.sliderContenerScrollView = scroll;
    titleView.buttonClick = ^(NSInteger index) {
        [scroll jumpTo:index];
    };
    scroll.didScroll = ^(NSInteger index) {
        [titleView selectButtonWithIndex:index];
    };
    
    
    
    
//    WDSliderContenerCollectionView *contenerView = [[WDSliderContenerCollectionView alloc] initWithFrame:CGRectMake(0, 150, SCREEN_WIDTH, 300)];
//    contenerView.childViews = @[self.redViewController.view,  self.blueViewController.view, self.greenViewController.view];
//    [self.view addSubview:contenerView];
//    self.sliderContenerView = contenerView;
//
//    titleView.buttonClick = ^(NSInteger index) {
//        [contenerView scrollToIndex:index];
//    };
//    contenerView.didScroll = ^(NSInteger index) {
//        [titleView selectButtonWithIndex:index];
//    };
    
    
    
    
    
//    SPScrollPageView *scrollPage = [SPScrollPageView scrollPageViewWithPageCount:self.childControllers.count initialIndex:0 frame:CGRectMake(0, 150, SCREEN_WIDTH, 300)];
//    scrollPage.sp_delegete = self;
//    [self.view addSubview:scrollPage];
//    titleView.buttonClick = ^(NSInteger index) {
//        NSLog(@"点击第%ld个button", index);
//        [scrollPage jumpToIndex:index animated:YES];
//    };
//    self.sliderTitleView = titleView;
//    self.scrollPageView = scrollPage;
}


- (UIView *)scrollPageView:(SPScrollPageView *)pageView pageForIndex:(NSInteger)index {
    UIView *view = [pageView dequeuePageViewWithIndex:index];
    if (!view) {
        if ([self.childControllers[index] isKindOfClass: [UIViewController class]]) {
            UIViewController *controller = (UIViewController *)self.childControllers[index];
            view = controller.view;
        }
    }
    return view;
}
- (void)scrollPageDidEndBounceAtPage:(UIView *)stillPage index:(NSInteger)index {
    [self.sliderTitleView selectButtonWithIndex:index];
}


- (void)addDismissButton {
    UIButton *dismiss = ({
        UIButton *btn = [[UIButton alloc] init];
        [btn setTitle:@"dismiss" forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        CGFloat f = 100;
        btn.frame = CGRectMake(SCREEN_WIDTH - f, SCREEN_HEIGHT - f, f, f);
        [btn setBackgroundColor:[UIColor purpleColor]];
        [btn addTarget:self action:@selector(dismissButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        btn;
    });
    [self.view addSubview:dismiss];
}
- (void)dismissButtonClick:(UIButton *)sender {
    [self dismissViewControllerAnimated:true completion:nil];
}





@end
